package com.ruoyi.api.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vim.domain.ImContacts;
import com.ruoyi.vim.service.IImContactsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 联系人Controller
 *
 * @author wgp
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/vim/contacts")
public class ImContactsController extends BaseController<ImContacts> {
    @Autowired
    private IImContactsService imContactsService;

    /**
     * 查询联系人列表
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:list')")
    @GetMapping("/list")

    /**
     * 列表
     * @param imContacts 实体
     * @param pageNum 页码
     * @param pageSize 每页多少条
     */
    public TableDataInfo list(ImContacts imContacts, Long pageNum, Long pageSize) {
        QueryWrapper<ImContacts> queryWrapper = getQueryWrapper(imContacts);
        IPage<ImContacts> iPage = imContactsService.page(new Page<ImContacts>(pageNum, pageSize), queryWrapper);
        return getDataTable(iPage);
    }

    /**
     * 导出联系人列表
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:export')")
    @Log(title = "联系人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImContacts imContacts) {
        QueryWrapper<ImContacts> queryWrapper = getQueryWrapper(imContacts);
        List<ImContacts> list = imContactsService.list(queryWrapper);
        ExcelUtil<ImContacts> util = new ExcelUtil<>(ImContacts.class);
        util.exportExcel(response, list, "联系人数据");
    }

    /**
     * 获取联系人详细信息
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId) {
        return AjaxResult.success(imContactsService.getById(userId));
    }

    /**
     * 新增联系人
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:add')")
    @Log(title = "联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImContacts imContacts) {
        return toAjax(imContactsService.save(imContacts));
    }

    /**
     * 修改联系人
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:edit')")
    @Log(title = "联系人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImContacts imContacts) {
        return toAjax(imContactsService.updateById(imContacts));
    }

    /**
     * 删除联系人
     */
    @PreAuthorize("@ss.hasPermi('vim:contacts:remove')")
    @Log(title = "联系人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds) {
        return toAjax(imContactsService.removeBatchByIds(Arrays.asList(userIds)));
    }

    private QueryWrapper<ImContacts> getQueryWrapper(ImContacts imContacts) {
        QueryWrapper<ImContacts> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(imContacts.getUserId())) {
            queryWrapper.eq("user_id", imContacts.getUserId());
        }
        if (StringUtils.isNotEmpty(imContacts.getChatUserId())) {
            queryWrapper.eq("chat_user_id", imContacts.getChatUserId());
        }
        if (imContacts.getSaveTime() != null) {
            queryWrapper.eq("save_time", imContacts.getSaveTime());
        }
        return queryWrapper;
    }
}
